package com.ruoyi.product.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.product.domain.ProductSpu;
import com.ruoyi.product.domain.ProductSpuDto;
import com.ruoyi.product.domain.dto.ProductDetailDto;
import com.ruoyi.product.mapper.ProductSpuMapper;
import com.ruoyi.product.service.IProductSpuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品SPUController
 *
 * @author liuhuizhong
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/product/productSpu")
@Tag(name = "商品SPU接口", description = "提供商品SPU操作")
public class ProductSpuController extends BaseController {
    @Autowired
    private IProductSpuService productSpuService;

    @Autowired
    private ProductSpuMapper productSpuMapper;

    /**
     * 查询商品SPU列表
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:list')")
    @Operation(summary = "查询商品SPU列表", description = "传入SPU查询对象，返回商品SPU列表")
    @GetMapping("/list")
    public AjaxResult list(ProductSpuDto productSpuDto) {
        List<ProductSpuDto> list = productSpuService.selectProductSpuList(productSpuDto);
        return success(list);
    }

    /**
     * 导出商品SPU列表
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:export')")
    @Log(title = "商品SPU", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出商品SPU列表", description = "传入SPU查询对象，返回商品SPU列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSpuDto productSpu) {
        List<ProductSpuDto> list = productSpuService.selectProductSpuList(productSpu);
        ExcelUtil<ProductSpuDto> util = new ExcelUtil<>(ProductSpuDto.class);
        util.exportExcel(response, list, "商品SPU数据");
    }

    /**
     * 获取商品SPU详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取商品详细信息", description = "")
    public AjaxResult selectProductSpuDetail(@PathVariable Long id) {
        ProductDetailDto productSpuDetail = productSpuService.selectProductSpuDetail(id);
        return success(productSpuDetail);
    }

    /**
     * 新增商品SPU
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:add')")
    @Log(title = "商品SPU", businessType = BusinessType.INSERT)
    @Operation(summary = "新增商品SPU", description = "传入SPU对象，返回新增结果")
    @PostMapping
    public AjaxResult add(@RequestBody ProductSpu productSpu) {
        return toAjax(productSpuService.insertProductSpu(productSpu));
    }

    /**
     * 修改商品SPU
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:edit')")
    @Log(title = "商品SPU", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改商品SPU", description = "传入SPU对象，返回修改结果")
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpu productSpu) {
        return toAjax(productSpuService.updateProductSpu(productSpu));
    }

    /**
     * 删除商品SPU
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:remove')")
    @Log(title = "商品SPU", businessType = BusinessType.DELETE)
    @Operation(summary = "删除商品SPU", description = "传入SPU ID，返回删除结果")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productSpuService.deleteProductSpuByIds(ids));
    }

}
